package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.CommentCo;
import com.engineering.elink.common.Constant;
import com.engineering.elink.common.ResultType;
import com.engineering.elink.domain.Comment;
import com.engineering.elink.domain.Praise;
import com.engineering.elink.service.CommentService;
import com.engineering.elink.util.MapConvert;

@RestController
@RequestMapping("/api/comment")
@Api(value = "评论接口")
public class CommentController {
	@Autowired
	private CommentService commentService;
	
	/**
	 * 获取资料的一级评论
	 * @param co
	 * @return
	 */
	@RequestMapping(value = "/getTopCommentsByFileId", method = RequestMethod.GET)
	@ApiOperation(value = "分页获取资料的一级评论")
	public Map<String, Object> getTopCommentsByFileId(@ModelAttribute @Valid CommentCo co) {
		return commentService.getTopCommentsByFileId(co);
	}
	
	/**
	 * 获取一级评论下的所有评论
	 * @param bc
	 * @return
	 */
	@RequestMapping(value = "/getAllCommentsByTopId", method = RequestMethod.GET)
	@ApiOperation(value = "分页获取一级评论下的评论")
	public Map<String, Object> getAllCommentsByTopId(@ModelAttribute @Valid BaseCo bc,
			@ApiParam(value="一级评论的id",required=true) @RequestParam("topId")int topId,
			@ApiParam(value="评论类型1--博文，2--企业，3--职位,默认1",required=true,defaultValue="1")
	@RequestParam(value="type",required=true,defaultValue="1")int type,
	@ApiParam(value="开始时间",required=false)@DateTimeFormat(pattern=Constant.DATE_FORMAT_YEAR)
	@RequestParam(value="startTime",required=false)Date startTime,
	@ApiParam(value="结束时间",required=false)@DateTimeFormat(pattern=Constant.DATE_FORMAT_YEAR)
	@RequestParam(value="endTime",required=false)Date endTime) {
		return commentService.getAllCommentsByTopId(bc,topId,type,startTime,endTime);
	}
	
	/**
	 * 添加评论
	 * @param id
	 * @param userId
	 * @param content
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiOperation(value = "发表评论")
	public Map<String, Object> add(@ModelAttribute @Valid Comment comment	,HttpServletRequest  request) {
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			if((int)userInfo.get("userType")==Constant.personUserType){//只有个人用户才能够评论
				String userId = (String)userInfo.get("userId");
				comment.setUserId(userId);
				return commentService.add(comment);
			}else{
				return MapConvert.getMap(ResultType.AUTHORITY_ERROR);
			}
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
		
		
	}
	
	/**
	 * 回复评论
	 * @param id
	 * @param commentId
	 * @param userId
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "/addReply", method = RequestMethod.POST)
	@ApiOperation(value = "回复评论")
	@SuppressWarnings("unchecked")
	public Map<String, Object> addReply(
			@ApiParam(value = "外键ID", required = true) @RequestParam("fkId") String fkId,
			@ApiParam(value = "评论id", required = true) @RequestParam("commentId") int commentId,
			@ApiParam(value = "评论内容", required = true) @RequestParam("content") String content,HttpServletRequest request) {
	
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			if((int)userInfo.get("userType")==Constant.personUserType){//只有个人用户才能够回复
				String userId = (String)userInfo.get("userId");
				return commentService.addReply(fkId, commentId, userId, content);
			}else{
				return MapConvert.getMap(ResultType.AUTHORITY_ERROR);
			}
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);

	}	
	
	/**
	 * 评论点赞(顶/踩)
	 * @param praise
	 * @return
	 */
	@RequestMapping(value = "addPraise", method = RequestMethod.POST)
	@ApiOperation(value = "评论点赞(顶/踩)")
	@SuppressWarnings("unchecked")
	public Map<String, Object> addPraise(@ModelAttribute @Valid Praise praise,HttpServletRequest request) {
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			if((int)userInfo.get("userType")==Constant.personUserType){//只有个人用户才能够回复
				String userId = (String)userInfo.get("userId");
				praise.setUserId(userId);
				return commentService.addPraise(praise);
			}else{
				return MapConvert.getMap(ResultType.AUTHORITY_ERROR);
			}
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}
	
	/**
	 * 获取评论我的
	 * @param count
	 * @return
	 */
	@RequestMapping(value="/commentForMe",method=RequestMethod.GET)
	@ApiOperation(value="获取评论我的")
	public 	Map<String, Object>commentForMe(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		return commentService.getCommentForMe(co,userId);
	}
	
	/**
	 * 清空评论记录
	 * @param type
	 * @return
	 */
	@RequestMapping(value="/delComment",method=RequestMethod.GET)
	@ApiOperation(value="清空评论",notes="清空记录，我的评论/评论我的")
	public 	Map<String, Object>delComment(@ApiParam(value = "清空类型,1--我的评论，2--评论我的", required = true,defaultValue="1")
	@RequestParam("type") int type,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		return commentService.delComment(userId,type);
	}
	
	/**
	 * 获取我评论的
	 * @param count
	 * @return
	 */
	@RequestMapping(value="/myComment",method=RequestMethod.GET)
	@ApiOperation(value="获取我评论的")
	public 	Map<String, Object>myComment(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		return commentService.getMyComment(co,userId);
	}
	
	
	
	/**
	 * 获取综合评价
	 * @param fkId
	 * @param startTime
	 * @param endTime
	 * @param type
	 * @return
	 */
	@RequestMapping(value="/getEvaluateById",method=RequestMethod.GET)
	@ApiOperation(value="获取综合评价",httpMethod="GET")
	public Map<String, Object>getEvaluateById(
			@ApiParam(value="主键，用户id或者公司id",required=true)@RequestParam(value="fkId",required=true)String fkId,
			@ApiParam(value="开始时间",required=false)@DateTimeFormat(pattern=Constant.DATE_FORMAT_YEAR)
			@RequestParam(value="startTime",required=false)Date startTime,
			@ApiParam(value="结束时间",required=false)@DateTimeFormat(pattern=Constant.DATE_FORMAT_YEAR)
			@RequestParam(value="endTime",required=false)Date endTime,
			@ApiParam(value="类型1--企业，,默认1",required=true,defaultValue="1")
			@RequestParam(value="type",required=true,defaultValue="1")int type
			){
		return commentService.getEvaluateById(fkId,startTime,endTime,type);
	}
	
	/**
	 * 获取最新点评
	 * @param count
	 * @return
	 */
	@RequestMapping(value = "getRecentComment", method = RequestMethod.GET)
	@ApiOperation(value = "获取最新点评",httpMethod="GET",notes="获取工程汇--最新点评")
	public Map<String, Object>getRecentComment(
			@ApiParam(value = "返回数据的条数", required = false,defaultValue="10") 
	@RequestParam(value = "count", required = false,defaultValue="10") int count){
		return MapConvert.getMap(ResultType.SUCCESS, commentService.getRecentComment(count)); 
	}	
}
